package com.my.content.ui_homework.fragment.FirstPage;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.my.content.ui_homework.Constant;
import com.my.content.ui_homework.R;
import com.my.content.ui_homework.fragment.FirstPage.Banners;
import com.my.content.ui_homework.fragment.FirstPage.FirstPageAdapter;
import com.my.content.ui_homework.fragment.FirstPage.FirstPages;
import com.my.content.ui_homework.helper.GlideImageLoader;
import com.my.content.ui_homework.helper.NoScrollListView;
import com.my.content.ui_homework.helper.OkHttpClientHelper;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstPagerFragment extends Fragment {


    private Banner mBanner;
    private NoScrollListView mLv;
    private FirstPageAdapter mAdapter;
    private List<Banners.AdListBean> topList;
    private List<FirstPages.RowsBean> mList;
    private List<String> mList_imageUri = new ArrayList<>();
    private List<String> mList_titleUri = new ArrayList<>();



    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if( msg.what == 1) {
//                //图片轮滑数据传递
                topList = (List<Banners.AdListBean>) msg.obj;
                for (int i = 0; i < topList.size(); i++) {
                    mList_imageUri.add(topList.get(i).getImgUrl());
                }
                mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
                mBanner.setImageLoader(new GlideImageLoader());
                //设置图片集合
                mBanner.setImages(mList_imageUri);
                //设置标题集合（当banner样式有显示title时）
                mBanner.setBannerTitles(mList_titleUri);
                //设置自动轮播，默认为true
                mBanner.isAutoPlay(true);
                //设置轮播时间
                mBanner.setDelayTime(3000);
                //设置指示器位置（当banner模式中有指示器时）
                mBanner.setIndicatorGravity(BannerConfig.CENTER);
                //banner设置方法全部调用完毕时最后调用
                mBanner.start();
//                mBanner.setOnBannerClickListener(new OnBannerClickListener() {
//                    @Override
//                    public void OnBannerClick(int position) {
//                        Intent intent = new Intent(getActivity(), WebViewActivity.class);
//                        String url = topList.get(position - 1).getShare_url();//把url需要的地址传过去
//                        intent.putExtra("url", url);
//                        startActivity(intent);
//                    }
//                });
            }else if(msg.what == 2){
                mList = (List<FirstPages.RowsBean>) msg.obj;
                mAdapter.setData(mList);
            }
//            mLv.onRefreshComplete();
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first_pager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBanner = (Banner) view.findViewById(R.id.first_banner);
        mLv = (NoScrollListView) view.findViewById(R.id.first_lv);
        mAdapter = new FirstPageAdapter(getActivity());
        mLv.setAdapter(mAdapter);
        initData();
    }

    //加载解析数据
    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String beanJson = OkHttpClientHelper.getStringFromURL(getActivity(), Constant.FIRST_LISTVIEW_URL, null);
                    Log.i("tag","___________________________________!!!!!!!!!!!"+beanJson);
                    FirstPages bean = mAdapter.parseJsontoBean(beanJson);
                    final List<FirstPages.RowsBean> mList = bean.getRows();
                    Log.i("tag","22222222222222222222222222"+mList);

                    String bannJson = OkHttpClientHelper.getStringFromURL(getActivity(), Constant.FIRST_BANNER_URL, null);
                    Banners bann = parseJsontoBean(bannJson);
                    final List<Banners.AdListBean> topList = bann.getAdList();
                    Log.i("tag","33333333333333333333333333333333333333333333"+topList);

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Message message = Message.obtain();
                            message.what = 1;
                            message.obj = topList;
                            mHandler.sendMessage(message);
                            Message message1 = Message.obtain();
                            message1.what = 2;
                            message1.obj = mList;
                            mHandler.sendMessage(message1);
                        }
                    });
                } catch (IOException e) {
                }
            }
        }).start();
    }
    public Banners parseJsontoBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<Banners>() {
        }.getType());
    }
}
